﻿namespace Blaze.Editor
{
    using System.IO;
    using FluentFTP;
    using Framework;

    /// <summary>
    /// 为<see cref="FtpClient"/>提供扩展功能。
    /// </summary>
    public static class FtpClientExtension
    {
        /// <summary>
        /// 从Ftp上下载整个文件夹。
        /// </summary>
        /// <param name="ftpClient">Ftp客户端</param>
        /// <param name="remotePath">远程目录</param>
        /// <param name="localPath">下载目录</param>
        /// <param name="topOnly">是否仅下载父文件夹</param>
        public static void DownloadDirectory(this FtpClient ftpClient, string remotePath, string localPath, bool topOnly = false)
        {
            foreach (var item in ftpClient.GetListing(remotePath))
            {
                switch (item.Type)
                {
                    case FtpFileSystemObjectType.Directory:
                        if (topOnly)
                            continue;
                        FileUtility.EnsureDirectory(localPath);
                        ftpClient.DownloadDirectory(Path.Combine(remotePath, item.FullName), localPath);
                        break;
                    case FtpFileSystemObjectType.File:
                        var target = localPath + "/" + item.Name;
                        ftpClient.DownloadFile(target, item.FullName, true, FtpVerify.Retry);
                        break;
                    case FtpFileSystemObjectType.Link:
                        //暂不支持Link
                        continue;
                }
            }
        }
    }
}